var mongoose = require('mongoose'),
    uuid = require('uuid'),
    Schema = mongoose.Schema;
const envConfig = require('../../config/env/config_development');

var merchantSchema = new Schema({
    _id: {type:String},
    name: {type:String},
    storeName: {type:String},
    password: {type:String},
    idcard: {type:String},
    img: {type:String},
    address: {type:String},
    state: {type:Number},
    block: {type:Number},
    submitTime: {type: Date},
    verifyTime: {type: Date}
}, {
    collection: 'merchant'
});

// Indexes

// Auto update last update time
merchantSchema.pre('update', function () {
    this.update({}, { $set: { submitTime: new Date() } });
});

merchantSchema.pre('save', function (next) {
    var merchant = this;
    var mid=uuid.v4();
    merchant._id = mid;
    merchant.state=0;
    merchant.block=0;
    merchant.submitTime=new Date();
    merchant.idcard=envConfig.upload.idcard+mid+".jpg";
    merchant.img=envConfig.upload.img+mid+".jpg";
    next();
});

mongoose.model('Merchant', merchantSchema);